const Acan = require('../../libs/Acan')

// 格式化
const oneFormat = function (doc, type) {
  if (doc.optime) {
    doc.optime = Acan.time('Y-m-d H:i:s', doc.optime)
  }
  delete doc.__v
}

module.exports = {
  before: {
    all: [],
    find: [d => {
      var get = d.params.query
      // 实际范围搜索
      var start = {}
      var end = {}
      if (get.optimeStart) {
        start = {$gte: get.optimeStart + ' 00:00:00'}
        delete get.optimeStart
      }
      if (get.optimeEnd) {
        end = {$lte: get.optimeEnd + ' 23:59:59'}
        delete get.optimeEnd
      }
      get.optime = Object.assign(start, end)
      if (Acan.isEmpty(get.optime)) delete get.optime
      delete get.del
      // 排序
      if (!get.$sort) get.$sort = {}
      get.$sort._id = -1
    }],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  after: {
    all: [],
    find: [ d=> {
      if (!d.result.data) return
      d.result.data.forEach(doc => {
        oneFormat(doc, d.params.type)
      })
    } ],
    get: [ d=> {
      var doc = d.result
      oneFormat(doc, d.params.type)
    } ],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
}
